package com.call.data;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;

public abstract class ListByDatabase {
	
	private SQLiteOpenHelper m_objDBHelper;
	private String m_strTableName;
	
	public ListByDatabase(SQLiteOpenHelper objDBHelper, String strTableName)
	{
		m_strTableName = strTableName;
		m_objDBHelper = objDBHelper;
	}
	
	/*
	 * delete one item
	 * return the number of affected items
	 */
	public int delete( String selection, String[] selectionArgs)
	{
		SQLiteDatabase db = m_objDBHelper.getWritableDatabase();
		return db.delete(m_strTableName, selection, selectionArgs);
	}
	
	/*
	 * insert one item
	 * return the id of the item
	 */
	public long insert(ContentValues values)
	{
		SQLiteDatabase db = m_objDBHelper.getWritableDatabase();
		return db.insert(m_strTableName, "", values);
	}
	
	/*
	 * query the list
	 * return the cursor for go through the list
	 */
	public Cursor query(String[] projection, String selection,
			String[] selectionArgs, String sortOrder, int startLimit, int endLimit)
	{
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		SQLiteDatabase db = m_objDBHelper.getWritableDatabase();
		qb.setTables(m_strTableName);
		return qb.query(db,projection, selection, selectionArgs, null, null, sortOrder,
				startLimit + "," + endLimit);
	}
	
	/*
	 * update the list
	 * return the nubmer of affected items
	 */
	public int update(ContentValues values, String selection,
			String[] selectionArgs)
	{
		return 0;
	}
}
